<?php
function smarty_function_browse_results($params, &$smarty) {
	$results = $params["results"];
	
	$output = "";
	$prev_type = "";
	if( isset( $_SESSION["silk_user"]["id"] )) {
		$current_user = orm("SilkUser")->find_by_id( $_SESSION["silk_user"]["id"] );
		$friends = $current_user->get_friends();
	}
	
	foreach( $results as $result ) {
		if( $result->type != $prev_type ) {
			if( $output != "" ) {
				$output.="</table>";
			}
			$output.= "<h4>$result->type</h4>";
			
			$output.= "<table>";
			$prev_type = $result->type;
		}
		$user = $result->user;
		$output.= "<tr>";
		$output.= "<td>Name: <b>" . $user->first_name . " " . $user->last_name . "</br></td>";
		$output.= "<td>";
		if( isset( $_SESSION["silk_user"]["id"] )) {
			if( in_array( $user->id, $friends )) {
					$output.= "Friend";
			} else {
				if( $user->id == $_SESSION["silk_user"]["id"] ) {
					$output.= "It's you!";
				} else {
					$output.= "<a href='" . SilkResponse::create_url(array(	"controller" => "browse",
																			"action" => "add_friend",
																			"friend_id" => $user->id
																			)) .
								"'>Add Friend</a>";
				}
			}
		}
		$output.= "</td>";
		$output.= "</tr>";
		$output.= "<tr>";
		$output.= "<td>IMAGE</td>";
		$output.= "<td>$result->name ($result->id)</td>";
		$output.= "<td>$result->description</td>";
		$output.="</tr>";
	}
	$output.= "</table>";
	return $output;
}
?>